package com.zr.sms.servlet;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
/**
 * 打印购物小票功能
 * @author wangbs
 *
 */
public class DayinxiaopiaoServlet extends HttpServlet{
  @Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
		throws ServletException, IOException {
	doPost(req, resp);
}
  @Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
	  
		   req.setCharacterEncoding("utf-8");
		   String []gids=req.getParameterValues("gids");
		   StringBuffer gid=new StringBuffer("");
		 
	    	  for (int i = 0; i < gids.length-1; i++) {
				gid.append(gids[i]+",");
			}
	    	  gid.append(gids[gids.length-1]);
	    	
		   
		   String []gnames=req.getParameterValues("gnames");
		   StringBuffer gname=new StringBuffer("");
	    	  for (int i = 0; i < gids.length-1; i++) {
	    		  gname.append(gnames[i]+",");
			}
	    	  gname.append(gnames[gids.length-1]);
		   
		   String []gmoneys=req.getParameterValues("gmoneys");
		   StringBuffer gmoney=new StringBuffer("");
	    	  for (int i = 0; i < gids.length-1; i++) {
	    		  gmoney.append(gmoneys[i]+",");
			}
	    	  gmoney.append(gmoneys[gids.length-1]);
		   
		   String []gnumbers=req.getParameterValues("gnumbers");
		   StringBuffer gnumber=new StringBuffer("");
	    	  for (int i = 0; i < gnumbers.length-1; i++) {
	    		  gnumber.append(gnumbers[i]+",");
			}
	    	  gnumber.append(gnumbers[gids.length-1]);
		   String []gprices=req.getParameterValues("gprices");
		   StringBuffer gprice=new StringBuffer("");
	    	  for (int i = 0; i < gnumbers.length-1; i++) {
	    		  gprice.append(gprices[i]+",");
			}
	    	  gprice.append(gprices[gids.length-1]);
	  String sumstr=gid.toString()+"#"+gname.toString()+"#"+gnumber.toString()+"#"+gprice.toString()+"#"+gmoney.toString();	  
	  dachu(sumstr,resp,req);
      req.getRequestDispatcher("cashier/sycz.jsp");    
}
  public void dachu(String data,HttpServletResponse resp,HttpServletRequest req) {
	     resp.setContentType("application/octet-stream"); //重点
		  resp.setCharacterEncoding("utf-8");//重点
		  String name = "order.xls";
		  String[] gids=req.getParameterValues("gids");
		  try {
			name = URLEncoder.encode(name, "utf-8");//重点
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		  resp.addHeader("Content-Disposition", "attachment;filename=" + name);//重点
      
		  
       String[] arr1 = data.split("#");
   
       String[][] arr3 = new String[gids.length+1][5];
		    for (int i = 0; i < 5; i++) {
		    String arr2[] = arr1[i].split(",");
			for (int j = 0; j < gids.length; j++) {
				
				arr3[j][i] = arr2[j];
			}
		}
		  
		WritableWorkbook wwb;
		try {
		//创建一个输出流
		OutputStream os = resp.getOutputStream();
		wwb=Workbook.createWorkbook(os);
		//创建一个工作页，第一个参数的页名，第二个参数表示该工作页在excel中处于哪一页
		WritableSheet sheet = wwb.createSheet("购物小票", 0);
		//设置所有列的列宽
		for (int i = 1; i<=7; i++) {
			sheet.setColumnView(i, 15);
		}
		//写入表头
		Label label1 = new Label(0,0,"商品编号");//填充第一行第一个单元格的内容
		sheet.addCell(label1);
		Label label2 = new Label(1,0,"商品名字");//填充第一行第一个单元格的内容
		sheet.addCell(label2);
		Label label3 = new Label(2,0,"购买数量");//填充第一行第一个单元格的内容
		sheet.addCell(label3);
		Label label4 = new Label(3,0,"商品单价");//填充第一行第一个单元格的内容
		sheet.addCell(label4);
		Label label5 = new Label(4,0,"合计价格");//填充第一行第一个单元格的内容
		sheet.addCell(label5);	
		//循环写入表内容
		for (int i = 0; i < gids.length; i++) {
			for (int j = 0; j < 5; j++) {
				Label label = new Label(j,i+1,arr3[i][j]);//
				sheet.addCell(label);
			}
		}
		wwb.write();//将内容写到excel文件中
		os.flush();//清空输出流
		wwb.close();
	} catch (FileNotFoundException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (RowsExceededException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (WriteException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} 

  }
}
